/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.inf.ufg.view.cadastros;

import br.inf.ufg.model.Local;
import br.inf.ufg.model.LocalDAO;
import br.inf.ufg.model.TipoAssentos;
import br.inf.ufg.model.TipoAssentosDAO;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author umarley.faria
 */
public class TelaCadastroLocal extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastroLocal
     */
    public TelaCadastroLocal() {
        initComponents();
        iniciarBD();
        primeiro();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btFirst = new javax.swing.JButton();
        btPrev = new javax.swing.JButton();
        btNext = new javax.swing.JButton();
        btLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btDeletar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cbUf = new javax.swing.JComboBox();
        tfCodigo = new javax.swing.JTextField();
        tfNome = new javax.swing.JTextField();
        tfCapacidade = new javax.swing.JTextField();
        tfCep = new javax.swing.JTextField();
        tfRua = new javax.swing.JTextField();
        tfComplemento = new javax.swing.JTextField();
        tfBairro = new javax.swing.JTextField();
        tfCidade = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtAssentos = new javax.swing.JTable();
        jToolBar2 = new javax.swing.JToolBar();
        btAddAssentos = new javax.swing.JButton();
        btRemoverAssento = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Locais");
        setAlwaysOnTop(true);

        jToolBar1.setRollover(true);

        btFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_first.png"))); // NOI18N
        btFirst.setToolTipText("Primeiro Registro");
        btFirst.setFocusable(false);
        btFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFirstActionPerformed(evt);
            }
        });
        jToolBar1.add(btFirst);

        btPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_previous.png"))); // NOI18N
        btPrev.setToolTipText("Anterior");
        btPrev.setFocusable(false);
        btPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrevActionPerformed(evt);
            }
        });
        jToolBar1.add(btPrev);

        btNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_next.png"))); // NOI18N
        btNext.setToolTipText("Próximo");
        btNext.setFocusable(false);
        btNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextActionPerformed(evt);
            }
        });
        jToolBar1.add(btNext);

        btLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_last.png"))); // NOI18N
        btLast.setToolTipText("Último Registro");
        btLast.setFocusable(false);
        btLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLastActionPerformed(evt);
            }
        });
        jToolBar1.add(btLast);
        jToolBar1.add(jSeparator1);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btNovo);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/disk.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btSalvar);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/pencil.png"))); // NOI18N
        btEditar.setToolTipText("Editar");
        btEditar.setFocusable(false);
        btEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btEditar);

        btDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btDeletar.setToolTipText("Remover");
        btDeletar.setFocusable(false);
        btDeletar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDeletar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeletarActionPerformed(evt);
            }
        });
        jToolBar1.add(btDeletar);

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/magnifier.png"))); // NOI18N
        btPesquisar.setToolTipText("Procurar");
        btPesquisar.setFocusable(false);
        btPesquisar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPesquisar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        jToolBar1.add(btPesquisar);

        jLabel1.setText("Código:");

        jLabel2.setText("Nome:");

        jLabel3.setText("Capacidade:");

        jLabel4.setText("CEP:");

        jLabel5.setText("Rua:");

        jLabel6.setText("Complemento:");

        jLabel7.setText("Bairro:");

        jLabel8.setText("Cidade:");

        jLabel9.setText("UF:");

        cbUf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "selecione", "AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE","SP", "TO"}));

        tfCodigo.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfCep, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 53, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfRua)
                                    .addComponent(tfBairro))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCidade))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCapacidade, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(36, 36, 36)
                        .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(34, 34, 34))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCapacidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(59, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Dados do Local", jPanel1);

        dtAssentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Codigo", "Tipo"
            }
        ));
        jScrollPane1.setViewportView(dtAssentos);

        jToolBar2.setRollover(true);

        btAddAssentos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btAddAssentos.setFocusable(false);
        btAddAssentos.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btAddAssentos.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(btAddAssentos);

        btRemoverAssento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btRemoverAssento.setFocusable(false);
        btRemoverAssento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btRemoverAssento.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(btRemoverAssento);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Assentos", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        novo();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFirstActionPerformed
        primeiro();
    }//GEN-LAST:event_btFirstActionPerformed

    private void btPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrevActionPerformed
        anterior();
    }//GEN-LAST:event_btPrevActionPerformed

    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextActionPerformed
        proximo();
    }//GEN-LAST:event_btNextActionPerformed

    private void btLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLastActionPerformed
       ultimo();
    }//GEN-LAST:event_btLastActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        editar();
    }//GEN-LAST:event_btEditarActionPerformed

    private void btDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeletarActionPerformed
        try {
            remover();
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroLocal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btDeletarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        pesquisar();
    }//GEN-LAST:event_btPesquisarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroLocal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCadastroLocal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddAssentos;
    private javax.swing.JButton btDeletar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btFirst;
    private javax.swing.JButton btLast;
    private javax.swing.JButton btNext;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btPrev;
    private javax.swing.JButton btRemoverAssento;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbUf;
    private javax.swing.JTable dtAssentos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JTextField tfBairro;
    private javax.swing.JTextField tfCapacidade;
    private javax.swing.JTextField tfCep;
    private javax.swing.JTextField tfCidade;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfComplemento;
    private javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfRua;
    // End of variables declaration//GEN-END:variables

    
        private LocalDAO localDao;
    
    
private void iniciarBD() {
        
        
        try {
             localDao = new LocalDAO();
        } catch ( Exception e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
 }    
    
public void mostrarDados( Local local ) {
    try{
        
            tfCodigo.setText(String.valueOf(local.getIdLocal()));
            tfNome.setText(local.getNome());
            tfCapacidade.setText(String.valueOf(local.getCapacidade()));
            tfCep.setText(local.getCep());
            tfRua.setText(local.getRua());
            tfComplemento.setText(local.getComplemento());
            tfBairro.setText(local.getBairro());
            tfCidade.setText(local.getCidade());
            cbUf.setSelectedItem( local.getEstado() );
            
            
            
            
         // System.out.println(Descricao);
            
        } catch (Exception e) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
        
 }


private void novo(){
    
            habilitaCampos();
    
            tfCodigo.setText("");
            tfNome.setText("");
            tfCapacidade.setText("");
            tfCep.setText("");
            tfRua.setText("");
            tfComplemento.setText("");
            tfBairro.setText("");
            tfCidade.setText("");
            cbUf.setSelectedItem( "" );
    
    
}


private void pesquisar(){
    
    
    
    String criterio = JOptionPane.showInputDialog( this, "Digite um critério para pesquisar");
    
    if(! criterio.isEmpty()){
        if(criterio.matches("^\\d+$")){
            int codigo = Integer.parseInt(criterio);

            try{
                Local dados = localDao.getLocalByCod(codigo);
                          if ( dados != null ) {
                              mostrarDados( dados );                    
                              JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                          } else {
                              JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                          }
              }catch(Exception e){
                  JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
              } 

            
        }else{
             try{
                Local dados = localDao.pesquisarLocal(criterio);  
                          if ( dados != null ) {
                              mostrarDados( dados );                    
                              JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                          } else {
                              JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                          }
              }catch(Exception e){
                  JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
              }


        }
    
   
    }
}

    /**
     *
     */
 public void primeiro() {
    desabilitaCampos();
        try {
                Local local = localDao.primeiro();

                if ( local != null) {
                    mostrarDados( local );
                }                    
            
        } catch ( SQLException e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
}
    
    public void anterior() {
        
            desabilitaCampos();
        
          try {
                Local local = localDao.anterior();
                 // System.err.println(assento);
                if ( local != null) {
                    mostrarDados( local );
                }                    
            
        } catch ( SQLException e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }
    
     public void proximo() {
         desabilitaCampos();
          try {
                Local local = localDao.proximo();

                if ( local != null) {
                    mostrarDados( local );
                }                    
            
        } catch ( SQLException e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }
     
      public void ultimo() {
          desabilitaCampos();
          try {
                Local local = localDao.ultimo();

                if ( local != null) {
                    mostrarDados( local );
                }                    
            
        } catch ( SQLException e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }
      
      
      public void salvar(){
          
          Local local = new Local();
          
          if(tfNome.getText().isEmpty()){
              
               JOptionPane.showMessageDialog( this, "Preencha o nome do local" );
              
          }else{
              
          
                if(tfCodigo.getText().isEmpty()){
                    local.setNome(tfNome.getText());
                    local.setCapacidade(Integer.parseInt(tfCapacidade.getText()));
                    local.setCep(tfCep.getText());
                    local.setRua(tfRua.getText());
                    local.setComplemento(tfComplemento.getText());
                    local.setBairro(tfBairro.getText());
                    local.setCidade(tfCidade.getText());
                    local.setEstado(cbUf.getSelectedItem().toString());
                    try{
                        localDao.salvarRegistro(local);
                        JOptionPane.showMessageDialog( this, "Salvo com Sucesso" );
                        primeiro();
                    }catch(Exception e){
                        JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                    }
                }else{
                    local.setIdLocal(Integer.parseInt( tfCodigo.getText() ));
                    local.setNome(tfNome.getText());
                    local.setCapacidade(Integer.parseInt(tfCapacidade.getText()));
                    local.setCep(tfCep.getText());
                    local.setRua(tfRua.getText());
                    local.setComplemento(tfComplemento.getText());
                    local.setBairro(tfBairro.getText());
                    local.setCidade(tfCidade.getText());
                    local.setEstado(cbUf.getSelectedItem().toString());
                    try{
                        localDao.atualizarRegistro(local);
                        JOptionPane.showMessageDialog( this, "Atualizado com Sucesso" );
                    }catch(Exception e){
                        JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                    }


                }
          }
          
      }
      
      private void editar(){
          
          habilitaCampos();
          
      }
      
      private void desabilitaCampos(){
            tfCodigo.setEditable(false);
            tfNome.setEditable(false);
            tfCapacidade.setEditable(false);
            tfCep.setEditable(false);
            tfRua.setEditable(false);
            tfComplemento.setEditable(false);
            tfBairro.setEditable(false);
            tfCidade.setEditable(false);
            cbUf.setEditable(false);
            
      }
      
      private void habilitaCampos(){
          // tfCodigo.setEditable(true);
            tfNome.setEditable(true);
            tfCapacidade.setEditable(true);
            tfCep.setEditable(true);
            tfRua.setEditable(true);
            tfComplemento.setEditable(true);
            tfBairro.setEditable(true);
            tfCidade.setEditable(true);
            cbUf.setEditable(true);
      }
      
      
      private void remover() throws SQLException{
          
          
          int resposta = JOptionPane.showConfirmDialog(rootPane, "Deseja Realmente Excluir este registro?", "Pergunta", JOptionPane.CANCEL_OPTION);
          
          if(resposta == JOptionPane.OK_OPTION){
              
              Local local = new Local();
              local.setIdLocal(Integer.parseInt( tfCodigo.getText()));
              
              try{
                  localDao.excluirRegistro(local);
                  primeiro();
              }catch(SQLException e){
                  JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
              }
              
              
          }
          
          
      }





}
